package com.gears_software.sxdc.dccx.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IVouchersFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.request.RRedeemcodeModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

/**
 * Created by chezhenzhen on 2017/11/22.
 */

@EActivity(R.layout.activity_redeemcode)
public class RedeemCodeActivity extends AppCompatActivity {

    @App
    DCCXApplication app;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView txt_tel_icon;
    @ViewById
    TextView txt_edit_icon;
    @ViewById
    EditText edit_code;
    @RestService
    IVouchersFinder vouchersFinder;
    @Bean
    GearsErrorHandler errorHandler;

    @AfterViews
    void created() {
        vouchersFinder.setRestErrorHandler(errorHandler);
        initIconFont();
    }

    private void initIconFont() {
        actionbar_title.setText("兑换码");
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        txt_tel_icon.setTypeface(app.iconfont);
        txt_edit_icon.setTypeface(app.iconfont);
    }

    @Click
    void btn_duihuan() {
        if (edit_code.getText().toString().trim().equals("")) {
            TastyToast.makeText(this, "请输入您的兑换码", app.SHORT, app.WARNING);
        } else {
            String code = edit_code.getText().toString().trim().toUpperCase();
            Logger.d(code);
            RRedeemcodeModel redeemcodeModel = new RRedeemcodeModel();
            redeemcodeModel.setRedeemcode(code);
            getDuiHuan(redeemcodeModel);
        }
    }

    @Background
    void getDuiHuan(RRedeemcodeModel redeemcodeModel) {
        ResultModel<String> res = vouchersFinder.exChangeVoucher(redeemcodeModel);
        showResult(res);

    }

    @UiThread
    void showResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(this, res.getData(), app.SHORT, app.SUCCESS);
                finish();
            } else {
                TastyToast.makeText(this, res.getErrmsg(), app.SHORT, app.ERROR);
            }
        }

    }

    @Click
    void back_icon() {
        this.finish();
    }
}
